const Item = require('./datbase');
console.log(Item.loadAllItems());
module.exports = main;
function loadPromotions() {
    return [
        {
            type: 'BUY_TWO_GET_ONE_FREE',
            barcodes: [
                'ITEM000000',
                'ITEM000001',
                'ITEM000005'
            ]
        }
    ];
}
function  main(inputs){
    let allItems = Item.loadAllItems();
    let promotions = Item.loadPromotions()[0].barcodes ;
    let obj_inputs = Obj_inputs(inputs);
    let obj_promotion = obj_promotions(obj_inputs,promotions);
    let arr = str_shop(obj_inputs,allItems,obj_promotion);
    let brr=pro(obj_inputs,allItems,obj_promotion);
    let str = '***<没钱赚商店>购物清单***\n' +arr[0]+
    '----------------------\n' +
    '挥泪赠送商品：\n' +brr[0]+
    '----------------------\n' +
    `总计：${arr[1]}(元)\n` +
    `节省：${brr[1]}(元)\n` +
    '**********************';
    console.log(str) ;
    return 'Hello world';
}
function Obj_inputs(inputs){
    let obj_inputs = {};
    for(let item of inputs){
        if(item.length !== 10){ 
            let arr = item.split('-');
            if(!obj_inputs[arr[0]]){
                obj_inputs[arr[0]] = arr[1] ;
            }
            else{
                obj_inputs[arr[0]] += arr[1] ;
            }
        }else{
            if(!obj_inputs[item]){
                obj_inputs[item] = 1 ;
            }
            else{
                obj_inputs[item] ++ ;
            }   
        }
    }
    return obj_inputs ;
}
function obj_promotions(obj_inputs,promotions){
    let obj = {} ;
        for(let key in obj_inputs){
            if(promotions.indexOf(key) !== -1){
            obj[key] = parseInt(obj_inputs[key]/3) ;
        }
    }
    return obj ;
}
function pro(obj_inputs,allItems,obj_promotion){
    let final_pro = '';
    let saved = 0 ; 
    let pro_brr=[];
    for(let item of allItems){
        for(let key in obj_inputs){
            if(item.barcode === key){
               if(obj_promotion[key]){
                saved += item.price * obj_promotion[key] ;
                final_pro += `名称：${item.name}，数量：${obj_promotion[key]}${item.unit}\n` ;  
                }
            }
        }
    }
    pro_brr.push(final_pro); 
    pro_brr.push(saved.toFixed(2));
    return pro_brr;
}
function str_shop(obj_inputs,allItems,obj_promotion){
    let final_pay = '' ;
    let final_arr = [] ;
    let total= 0;      
    for(let item of allItems){
        for(let key in obj_inputs){
            if(item.barcode === key){
                if(obj_promotion[key]){
                    let final_price= item.price * (obj_inputs[key] - obj_promotion[key]) ;
                    total += final_price ;
                    final_pay +=  `名称：${item.name}，数量：${obj_inputs[key]}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${final_price.toFixed(2)}(元)\n`
                }
                else{
                    let final_price= item.price * (obj_inputs[key]) ; 
                    total += final_price ;
                    final_pay +=  `名称：${item.name}，数量：${obj_inputs[key]}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${final_price.toFixed(2)}(元)\n`
                }
            }
        }
    }
   final_arr.push(final_pay);
//    final_arr.push(final_pro);
   final_arr.push(total.toFixed(2)) ;
//    final_arr.push(saved.toFixed(2));
    return final_arr ;
}